<template>
	<view>
      <view class="header">
        <view class="num">{{ distributionBonus }}</view>
        <view>账户金额</view>
      </view>
      <view>
        <view class="from">
          <view class="flex">银行卡号：<input type="text" v-model="account"></view>
          <view class="flex">真实姓名：<input type="text" v-model="name"></view>
          <view class="flex">提现金额：<input type="text" @blur="getMoney"></view>
          <view class="flex">手续费：<span style="color: red;float: right;">{{commissionMoney}}</span></view>
          <view class="tip">提现手续费为2%</view>
          <view class="flex">提现类型:
             <picker mode="selector" :range="typeList" range-key="name" @change="changeType" :value="index">
              <view>{{typeList[index].name?typeList[index].name:'请选择'}}</view>
            </picker>
          </view>
          <view v-if="index==0">
            <view class="flex">个税：<span style="color: red;float: right;">{{money}}</span></view>
            <!-- <view class="flex">到账金额：<span style="color: red;float: right;">{{commissionMoney}}</span></view> -->
            <view class="tip">
              <div>
                <span>1.当月提现金额未超过5000元,不收取个人所得税</span><br>
                <span>2.金额 {{moneyObj.needToPayTaxes}}元 需收取个人所得税</span><br>
                <span>3.费率 {{moneyObj.taxRate}}</span>
              </div>
            <!--  <div v-if="moneyObj.type == '1'">
                <span>1.本次提现部分超过5000元</span><br>
                <span>2.金额 {{moneyObj.needToPayTaxes}}元 需收取个人所得税</span><br>
                <span>3.费率 {{moneyObj.taxRate}}</span>
              </div>
              <div v-if="moneyObj.type == '2'">
                <span>1.本次提现全部超过5000元</span><br>
                <span>2.金额 {{moneyObj.needToPayTaxes}}元 需收取个人所得税</span><br>
                <span>3.费率 {{moneyObj.taxRate}}</span>
              </div> -->

            </view>
          </view>
          <view v-if="index==1" class="form1">
            <view class="flex">营业执照：
             <picker mode="selector" :range="photoList" range-key="name" @change="changeType1" :value="index1">
                <view>{{photoList[index1].name?photoList[index1].name:'请选择'}}</view>
              </picker>
            </view>
            <view class="flex">营业执照：
                <view class="bg-img" v-if="imgObj.businessLicenseImg" style="width: 200rpx;height: 200rpx;">
                   <!-- @tap="preview(imgObj.businessLicenseImg)" -->
                  <image :src='imgObj.businessLicenseImg' mode='aspectFill' style="width: 100%;height: 100%;"></image>
                  <view class="cu-tag bg-red" @tap="deleteImage(0)" :data-index="index">
                     <uni-icons type="clear" size="20" color="red"></uni-icons>
                  </view>
                </view>
                <view class="solids" @tap="ChooseImage" v-if="imgObj.businessLicenseImg==''">
                  <view class="scam">
                      <uni-icons type="camera" size="50"  color="#eee"></uni-icons>
                  </view>
                </view>
              </view>
              <view class="flex">对公开户许可证：
               <!-- @tap="preview(imgObj.corporateAccountOpeningLicenseImg)" -->
                  <view class="bg-img" v-if="imgObj.corporateAccountOpeningLicenseImg" style="width: 200rpx;height: 200rpx;">
                    <image :src='imgObj.corporateAccountOpeningLicenseImg' mode='aspectFill' style="width: 100%;height: 100%;" ></image>
                    <view class="cu-tag bg-red" @tap="deleteImage(1)" :data-index="index">
                     <uni-icons type="clear" size="20" color="red"></uni-icons>
                    </view>
                  </view>
                  <view class="solids" @tap="ChooseImage1" v-if="imgObj.corporateAccountOpeningLicenseImg==''">
                    <view class="scam">
                        <uni-icons type="camera" size="50"  color="#eee"></uni-icons>
                    </view>
                  </view>
                </view>
          </view>

           <view class="bnt" @tap="submit">申请提现</view>
        </view>
      </view>
	</view>
</template>

<script setup>
import { onLoad, onShow } from '@dcloudio/uni-app';
const picUrl = import.meta.env.VITE_APP_RESOURCES_URL
const baseUrl = import.meta.env.VITE_APP_BASE_API
const commissionMoney=ref('')
const cashmoney=ref('')
const moneyObj=ref('')
const distributionBonus=ref('')
const account=ref('')
const name=ref('')
const index=ref(0)
const index1=ref(0)
const typeList =ref([])
const photoList =ref([])
const imgObj=ref({})
// export default {
// 		data() {
// 			return {
//         // distributionBonus:'',
//         // commissionMoney:'',
//         // cashmoney:'',
//         // money:'',
//         // account:'',
//         // name:'',
//         // index:0,
//         // index:1,
// 			 typeList:[{
//           type:0,
//           name:'满5000需要缴纳个人所得税 '
//         },
//         {
//           type:1,
//           name:' 需上传营业执照和提现发票'
//         }]
// 			}
// 		},

// }
onLoad(()=>{
  distributionBonus.value =uni.getStorageSync('info').distributionBonus

})
onShow(()=>{
   let arr=[{
      type:0,
      name:'满5000需要缴纳个人所得税 '
    },
    {
      type:1,
      name:' 需上传营业执照和提现发票'
    }]
    typeList.value=arr
    let obj={ businessLicenseImg:'',
              corporateAccountOpeningLicenseImg:'',
        }
        imgObj.value=obj
})
const changeType = (e) =>{
  index.value=e.detail.value
  if(index.value==1){
    getphotoList()
 }

}
const deleteImage=(v)=>{
  if(v==0){
    imgObj.value.businessLicenseImg=''
  }else{
    imgObj.value.corporateAccountOpeningLicenseImg=''
  }
}
const  changeType1=(e)=>{
  index1.value=e.detail.value
  let arr=photoList.value
  let t=arr[index1.value].businessLicense
  if(t!=''){
    let img=JSON.parse(t)
    imgObj.value.businessLicenseImg=img.businessLicenseImg
    imgObj.value.corporateAccountOpeningLicenseImg=img.corporateAccountOpeningLicenseImg
  }else{
    imgObj.value.businessLicenseImg=''
    imgObj.value.corporateAccountOpeningLicenseImg=''
  }

}
const getMoney = (e) =>{
  let money=e.detail.value
  if(money > distributionBonus){
    uni.showToast({
      title: '提现金额不能大于账户金额',
      icon: 'fail'
    })
    return
  }
  http.request({
    url: '/p/user/cashOut/getServiceCharge',
    method: 'GET',
    data: {
      cashMoney:money
    }
  })
   .then(({ data }) => {
     commissionMoney.value = data
     cashmoney.value = money
     if(index.value==0){
       getTaxMoney()
     }
    })
}
const preview=(img)=>{
     uni.previewImage({
       urls:[img], //可以展示imgUrl 列表中所有的图片
       current:1 //首先当前index对应图片，左右可以切换其他序号的图片
      });
   }
const getTaxMoney = () =>{

  http.request({
    url: '/p/user/cashOut/calculateIndividualIncomeTax',
    method: 'GET',
    data: {
      uid:uni.getStorageSync('info').userId,
     cashOutMoney:cashmoney.value
    }
  })
   .then(({ data }) => {
     console.log(data)
     moneyObj.value=data
    })
}
const getphotoList= () =>{
  http.request({
    url: '/p/user/cashOut/getBusinessLicenseByCashOutHistory',
    method: 'GET',
    data: {
      uid:uni.getStorageSync('info').userId
    }
  })
   .then(({ data }) => {
     console.log(data)
      photoList.value=data
      let img=JSON.parse(data[0].businessLicense)
      imgObj.value.businessLicenseImg=img.businessLicenseImg
       imgObj.value.corporateAccountOpeningLicenseImg=img.corporateAccountOpeningLicenseImg
       console.log(imgObj.value)
    })
}
const ChooseImage=(e)=>{
  console.log(baseUrl.value)
  let token=uni.getStorageSync('Token')
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        console.log(res);
         uni.uploadFile({
          //url:"http://192.168.1.5:8080/file/upload",
          //url:"https://zhwy.pengyuevip.com/gateway/file/upload",
           url:baseUrl + "/user/file/upload/element",
           filePath:res.tempFilePaths[0],
           name:'file',
           header: { 'Authorization': 'Bearer '+   token },
           success(res) {
             let url=JSON.parse(res.data).data;
             imgObj.value.businessLicenseImg=picUrl  + url;
           }
         })
      }
    });
  }
  const ChooseImage1=(e)=>{
    let token=uni.getStorageSync('Token')
      wx.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (res) => {
           uni.uploadFile({
            //url:"http://192.168.1.5:8080/file/upload",
            //url:"https://zhwy.pengyuevip.com/gateway/file/upload",
             url:baseUrl + "/user/file/upload/element",
             filePath:res.tempFilePaths[0],
             name:'file',
             header: { 'Authorization': 'Bearer '+   token },
             success(res) {
               console.log(res)
               let url=JSON.parse(res.data).data;
               imgObj.value.corporateAccountOpeningLicenseImg=picUrl + url;
             }
           })
        }
      });
    }
const submit=()=>{
  let payeeInfo={
    account:account.value,
    name:name.value,
  }
  let actualDeductMoney=''
  if(index.value==1){
    actualDeductMoney=cashmoney.value
  }else{
     actualDeductMoney=parseFloat(cashmoney.value) + parseFloat( moneyObj.value.individualIncomeTax)
  }
  let data={
    userAccountTable: "tz_user",
    userAccountTableId: uni.getStorageSync('info').userId,
    userAccountTableField: "distribution_bonus",
    type: "BankCard", //WeChat Alipay
    payeeInfo: JSON.stringify(payeeInfo), //收款人账号姓名
    cashMoney: cashmoney.value, //提现金额
    commissionMoney:String(commissionMoney.value) ,
    state:0,
    userType: uni.getStorageSync('info').userLevel==0?'member':'leader',
    actualReceiptMoney:cashmoney.value - commissionMoney.value,
    actualDeductMoney: actualDeductMoney,
    individualIncomeTax: moneyObj.value.individualIncomeTax,
    businessLicenseJson:JSON.stringify(imgObj.value) ,
    cashOutType:index.value
  }
  console.log(data)

  http.request({
    url: '/p/user/cashOut',
    method: 'POST',
    data: {
        userAccountTable: "tz_user",
        userAccountTableId: uni.getStorageSync('info').userId,
        userAccountTableField: "distribution_bonus",
        type: "BankCard", //WeChat Alipay
        payeeInfo: JSON.stringify(payeeInfo), //收款人账号姓名
        cashMoney: cashmoney.value, //提现金额
        commissionMoney:String(commissionMoney.value) ,
        state:0,
        userType: uni.getStorageSync('info').userLevel==0?'member':'leader',
        actualReceiptMoney:cashmoney.value - commissionMoney.value,
        actualDeductMoney: actualDeductMoney,
        individualIncomeTax: moneyObj.value.individualIncomeTax,
        businessLicenseJson:JSON.stringify(imgObj.value) ,
        cashOutType:index.value
    }
  })
   .then(({ data }) => {
      console.log(data)
    if(data==true){
      uni.showToast({
        title: '提现申请成功',
        icon: 'success'
      })
      setTimeout(() => {
       uni.switchTab({
         url:"/pages/user/user"
       })
      }, 1200)
    }
    })
}
</script>

<style  scoped lang="scss">
  @use './cash.scss';

</style>
